package com.eagle.scali.usercenter.service.user;


import com.eagle.scali.usercenter.dao.bonus.BonusEventLogMapper;
import com.eagle.scali.usercenter.dao.user.UserMapper;
import com.eagle.scali.usercenter.domain.dto.content.ShareDTO;
import com.eagle.scali.usercenter.domain.dto.messaging.UserAddBonusMsgDTO;
import com.eagle.scali.usercenter.domain.dto.user.UserLoginDTO;
import com.eagle.scali.usercenter.domain.entity.bonus.BonusEventLog;
import com.eagle.scali.usercenter.domain.entity.user.User;
import com.eagle.scali.usercenter.feignclient.ContentCenterFeignClient;
import com.google.common.collect.Lists;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class UserService {
    private final UserMapper userMapper;

	private final BonusEventLogMapper bonusEventLogMapper;

	@Autowired
	private ContentCenterFeignClient contentCenterFeignClient;

    public User findById(Integer id) {
        // select * from user where id = #{id}
        return this.userMapper.selectByPrimaryKey(id);
    }

	public List<BonusEventLog> findBonusLogById(Integer id) {
		BonusEventLog bonusEven = BonusEventLog.builder().userId(id).build();
		List<BonusEventLog> list = this.bonusEventLogMapper.select(bonusEven);
		return list;
	}

	public List<ShareDTO> findContributionsById(Integer id) {
		BonusEventLog bonusEven = BonusEventLog.builder().userId(id).event("CONTRIBUTE").build();

		List<BonusEventLog> list = this.bonusEventLogMapper.select(bonusEven);
		List<String> userIds = Lists.transform(list, input -> input.getId().toString());
//		List<UserDTO> userDTOList = new ArrayList<>();
//		list.stream().peek(s -> {
//			UserDTO userDTO = new UserDTO();
//			userDTO.setId(s.getUserId());
//			userDTOList.add(userDTO);
//		}).collect(Collectors.toList());

		List<ShareDTO> shareDtoList = this.contentCenterFeignClient.findShareListByUserIds(userIds);
		return shareDtoList;
	}


	public int updateUserById(User user) {
		return this.userMapper.updateByPrimaryKeySelective(user);
	}


	@Transactional(rollbackFor = Exception.class)
	public void addBonus(UserAddBonusMsgDTO msgDTO) {
		// 1. 为用户加积分
		Integer userId = msgDTO.getUserId();
		Integer bonus = msgDTO.getBonus();
		User user = this.userMapper.selectByPrimaryKey(userId);

		user.setBonus(user.getBonus() + bonus);
		this.userMapper.updateByPrimaryKeySelective(user);

		// 2. 记录日志到bonus_event_log表里面
		this.bonusEventLogMapper.insert(
				BonusEventLog.builder()
						.userId(userId)
						.value(bonus)
						.event(msgDTO.getEvent())
						.createTime(new Date())
						.description(msgDTO.getDescription())
						.build()
		);
		log.info("积分添加完毕...");
	}

	public User login(UserLoginDTO loginDTO, String openId){
		User user = this.userMapper.selectOne(
				User.builder()
						.wxId(openId)
						.build()
		);
		if (user == null) {
			User userToSave = User.builder()
					.wxId(openId)
					.bonus(300)
					.wxNickname(loginDTO.getWxNickname())
					.avatarUrl(loginDTO.getAvatarUrl())
					.roles("user")
					.createTime(new Date())
					.updateTime(new Date())
					.build();
			this.userMapper.insertSelective(
					userToSave
			);
			return userToSave;
		}
		return user;
	}
}
